package com.csust.travel.mapper;
import com.csust.travel.entity.Books;
import com.csust.travel.entity.Sellers;
import com.csust.travel.entity.Transaction;
import com.csust.travel.entity.TransactionParams;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TransactionMapper {


    @Select("SELECT t.*, b.username AS buyer_name, s.username AS seller_name " +
            "FROM transactions t " +
            "LEFT JOIN new_users b ON t.buyer_id = b.id " +
            "LEFT JOIN new_users s ON t.seller_id = s.id " +
            "ORDER BY t.transaction_date DESC")
    List<Transaction> findAllItem();

    @Select("select * from transactions where id = #{string} or buyer_id =#{string} or seller_id =#{string}")
    List<Transaction> findByString(String queryString);

    @Select("select buyer_id from transactions where id = #{id}")
    Integer findBuyerId(Integer id);

    @Select("select seller_id from transactions where id = #{id}")
    Integer findSellerId(Integer id);

    @Select("select books_id from transactions where id = #{id}")
    Integer findBookId(Integer id);

    @Select("select username from new_users where id = #{id}")
    String findName(Integer id);

    @Select("select college from new_users where id = #{id}")
    String findCollege(Integer id);

    @Select("select major from new_users where id = #{id}")
    String findMajor(Integer id);

    @Select("select book_code from books where id =#{id} ")
    String findInfoByIdb1(Integer id);

    @Select("select name from books where id =#{id} ")
    String findInfoByIdb2(Integer id);

    @Select("select book_image from books where id =#{id} ")
    String findImgById(Integer id);


    @Delete("delete from transactions where id = #{id}")
    void delItem(Integer id);

    @Insert("insert into transactions (buyer_id,seller_id,books_id, quantity, transaction_date,amount) values (#{buyer_id},#{seller_id},#{book_id}, #{purchaseQuantity}, now(), #{totalAmount})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void createNewTransaction(TransactionParams item);

    @Select("select transaction_date from transactions where id = #{id}")
    String findTransaction_date(Integer id);

    @Select("select amount from transactions where id =#{id}")
    Double findAmount(Integer id);

    @Select("select quantity from transactions where id =#{id}")
    Integer findQuantity(Integer id);

    @Update("update transactions set hasCommented = 1 where id = #{item}")
    void setHasCommented(Integer item);

    @Update("update transactions set comment_id = #{num2} where id = #{num1}")
    void setCommentId(Integer num1,Integer num2);

    @Select("select * from transactions where buyer_id =#{id} order by transaction_date desc")
    List<Transaction> findBuy(Integer id);

    @Select("select * from transactions where seller_id =#{id} order by transaction_date desc")
    List<Transaction> findSell(Integer id);

    @Select("select * from transactions where buyer_id =#{id} and hasCommented = 0  order by transaction_date desc")
    List<Transaction> findNoReview(Integer id);
}
